package com.sxt.practiceapplication.activities;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.SplashActivity;
import com.sxt.practiceapplication.base.BaseActivity;
import com.sxt.practiceapplication.utils.CacheUtils;
import com.sxt.practiceapplication.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseActivity {

    private ViewPager viewPager;
    private Button btn_start_main;
    private LinearLayout ll_point_group;
    private List<ImageView> imageViews;
    private ImageView iv_red_point;
    private int leftMax;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
        initViewPager();
    }

    private void initViewPager() {
        int[] ids = new int[]{
                R.drawable.guide_1,
                R.drawable.guide_2,
                R.drawable.guide_3,
        };
        for (int i = 0; i < ids.length; i++) {
            ImageView imageView = new ImageView(this);
            //设置背景
            imageView.setBackgroundResource(ids[i]);
            //添加到集合中
            imageViews.add(imageView);

            //创建点
            ImageView point = new ImageView(this);
            point.setBackgroundResource(R.drawable.point_normal);
            float screenDensity = DisplayUtil.getScreenDensity(GuideActivity.this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DisplayUtil.dip2px(10, screenDensity), DisplayUtil.dip2px(10, screenDensity));
            if (i != 0) {
                //不包括第0个所有的点距离左边十个像素
                params.leftMargin = DisplayUtil.dip2px(10, screenDensity);
            }
            point.setLayoutParams(params);
            //添加到线性布局里面
            ll_point_group.addView(point);
        }
        //设置viewPager的适配器
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imageViews.size();
            }

            /**
             *
             * @param view 当前创建的视图
             * @param object instantiateItem方法返回的结果值
             */
            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            /**
             *
             * @param container viewPager
             * @param position 要创建的页面的位置
             * @return 返回和创建当前页面关系的值
             */
            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                ImageView imageView = imageViews.get(position);
                container.addView(imageView);
                return imageView;
            }

            /**
             *
             * @param container viewPager
             * @param position 要销毁的页面的位置
             */
            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });

        //根据view的声明周期，当试图执行到onLayout或者onDraw的时候
        // 视图的高和宽边距都有了
        iv_red_point.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                iv_red_point.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                leftMax = ll_point_group.getChildAt(1).getLeft() - ll_point_group.getChildAt(0).getLeft();
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int leftMargin = (int)(positionOffset * leftMax) + (position * leftMax);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_red_point.getLayoutParams();
                params.leftMargin = leftMargin;
                iv_red_point.setLayoutParams(params);

            }

            @Override
            public void onPageSelected(int position) {

                if (position == imageViews.size() - 1) {
                    btn_start_main.setVisibility(View.VISIBLE);
                }else {
                    btn_start_main.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        viewPager = findViewById(R.id.view_pager);
        btn_start_main = findViewById(R.id.btn_start_main);
        ll_point_group = findViewById(R.id.ll_point_group);
        imageViews = new ArrayList<>(3);
        iv_red_point =findViewById(R.id.iv_red_point);

        btn_start_main.setOnClickListener(v -> {
            startActivity(new Intent(this, MainActivity.class));
            //保存曾进入过主页面
            CacheUtils.putBoolean(this, SplashActivity.START_MAIN, true);
            finish();
        });
    }


}